#include "common.hpp"
#include "log.hpp"

int main()
{
    Init init;
    Log log(ONE_FILE);
    // 打开信道
    int fd = open(FIFO_FILE, O_RDONLY);
    if(fd == -1) {
        // perror("open");
        log(FATAL, "open file error, error string: %s, error code: %d", strerror(errno), errno);
        exit(FIFO_OPEN_ERR);
    }
    // cout << "server open file done" << endl;
    // 测试日志功能
    log(INFO, "server open file done");
    log(FATAL, "server open file done");
    log(WARNING, "server open file done");
    log(DEBUG, "server open file done");
    // 开始通信, 从管道中读数据
    while(true) {
        char buffer[N] = {0};
        int x = read(fd, buffer, sizeof(buffer));
        if(x > 0) {
            buffer[x] = 0;
            cout << "client say@ " << buffer << endl;
        }
        else if(x == 0) {
            // cout << "client quit, me too!" << endl;
            log(INFO, "client quit, me too!");
            break;
        }
        else {
            // perror("read");
            log(FATAL, "read file error, error string: %s, error code: %d", strerror(errno), errno);
            exit(FIFO_READ_ERR);
        }
    }
    return 0;
}